package graph;

import java.util.*;
import java.io.*;

import zclTool.*;

public class DrawGraphMatrix {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// load 0204uniq
		HashSet<Integer> train01ids = new HashSet<Integer>();
		DelimitedReader dr = new DelimitedReader("trainids");
		String line[];
		while ((line = dr.read()) != null) {
			int a = Integer.parseInt(line[0]);
			train01ids.add(a);
		}
		dr.close();
		HashMap<Integer, Integer> name2id = new HashMap<Integer, Integer>();
		ArrayList<ArrayList<Integer>> clusters = new ArrayList<ArrayList<Integer>>();
		dr = new DelimitedReader("0204uniq_id_sort_no_p");
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				"drawMatrix"));
		OutputStreamWriter osw_b = new OutputStreamWriter(new FileOutputStream(
				"border"));
		line = dr.read();// give up the first 572 largest cluster
		int ln = 0;
		while ((line = dr.read()) != null) {
			ln++;
			// System.out.println("Cluster i\t"+ln+"\t"+line[0]);
			ArrayList<Integer> onecluster = new ArrayList<Integer>();
			String[] attids = line[1].split(",");
			for (int i = 0; i < attids.length; i++) {
				int a = Integer.parseInt(attids[i]);
				if (train01ids.contains(a)) {
					onecluster.add(a);
					// int b = name2id.size();
					// name2id.put(a, b);
					// System.out.print(b+" ("+a+") ");
				}
			}
			clusters.add(onecluster);
			
			System.out.println();
			
		}
		dr.close();
		Collections.sort(clusters, new Comparator<ArrayList<Integer>>() {
			public int compare(ArrayList<Integer> c1, ArrayList<Integer> c2) {
				return c2.size() - c1.size();
			}
		});
		for(int i = 0;i<100;i++){
			System.out.println(clusters.get(i).size());
		}
		for (int i = 0; i < 30; i++) {
			ArrayList<Integer> c = clusters.get(i);
			System.out.println("cluster " + i);
			for (int a : c) {
				int b = name2id.size();
				name2id.put(a, b);
				System.out.print(b + " (" + a + ") ");
			}
			System.out.println();
			osw_b.write(name2id.size()+"\n");
			if (name2id.size() > 3000)
				break;
		}

		dr = new DelimitedReader("train.reduced.repaired");
		while ((line = dr.read()) != null) {
			int id1 = Integer.parseInt(line[0]);
			int id2 = Integer.parseInt(line[1]);
			double val = Double.parseDouble(line[2]);
			if (name2id.containsKey(id1) && name2id.containsKey(id2) && val>0) {
				int nid1 = name2id.get(id1);
				int nid2 = name2id.get(id2);
				osw.write(nid1 + "\t" + nid2 + "\t" + val + "\n");
			}
		}
		osw.close();
		osw_b.close();

	}

}
